package com.yuji.aacar.controller.address.roomaddress;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;

import com.yuji.aacar.R;
import com.yuji.aacar.base.BaseActivity;
import com.yuji.aacar.base.Const;
import com.yuji.aacar.base.EventConst;
import com.yuji.aacar.bean.beans.Room;
import com.yuji.aacar.bean.response.GetRoomNameResponse;
import com.yuji.aacar.controller.adapter.RecyclerViewItemTouchListener;
import com.yuji.aacar.controller.adapter.RoomAdapter;
import com.yuji.aacar.core.cores.event.CoreEventCenter;
import com.yuji.aacar.core.cores.services.CoreServiceResponse;
import com.yuji.aacar.core.cores.utils.LogUtil;
import com.yuji.aacar.service.BaseServiceMediator;
import com.yuji.aacar.service.BaseServiceParams;
import com.yuji.aacar.widgets.autolayout.AutoRelativeLayout;
import com.yuji.aacar.widgets.pulltorefresh.PtrFrameLayout;
import com.yuji.aacar.widgets.pulltorefresh.core.PtrDefaultHandler2;
import com.yuji.aacar.widgets.pulltorefresh.core.WptrClassicFrameLayout;
import com.yuji.aacar.widgets.textview.PingFangTextView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <li>Package:com.yuji.aacar.controller.address</li>
 * <li>Author: lining </li>
 * <li>Date: 2018/3/6</li>
 * <li>Description:   </li>
 */

public class RoomAddressActivity extends BaseActivity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.back_image)
    AutoRelativeLayout backImage;
    @BindView(R.id.title_content)
    PingFangTextView titleContent;
    @BindView(R.id.back_login)
    PingFangTextView backLogin;
    @BindView(R.id.title_message_iv)
    ImageView titleMessageIv;
    @BindView(R.id.title_message_rl)
    AutoRelativeLayout titleMessageRl;
    @BindView(R.id.title_rl)
    AutoRelativeLayout titleRl;
    @BindView(R.id.layout_recyclerview)
    RecyclerView layoutRecyclerview;
    @BindView(R.id.layout_pull_to_refresh)
    WptrClassicFrameLayout layoutPullToRefresh;
    private ArrayList<Room> datas;
    private RoomAdapter adapter;

//    private int isFrist = 0;
    private String fromPage;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        setContentView(R.layout.activity_room);
        ButterKnife.bind(this);
        initTitle(true, "小区选择", null);
        super.onCreate(savedInstanceState);

        fromPage = getIntent().getStringExtra("from");

        initData();
        initList();
    }

    private void initList() {
        layoutPullToRefresh.setPullToRefresh(true);
        //阻力系数，阻力越大，能拉动范围越小，默认为1.7f
        layoutPullToRefresh.setResistance(1.7f);
        //系数越大，需拉动越大的距离才能刷新或加载，默认1.2f
        layoutPullToRefresh.setRatioOfHeaderHeightToRefresh(1.2f);

        layoutPullToRefresh.setPtrHandler(new PtrDefaultHandler2() {
            @Override
            public void onLoadMoreBegin(PtrFrameLayout frame) {
                layoutPullToRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layoutPullToRefresh.refreshComplete();
                    }
                }, 2000);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                layoutPullToRefresh.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        layoutPullToRefresh.refreshComplete();
                    }
                }, 2000);
            }
        });
        layoutPullToRefresh.setMode(PtrFrameLayout.Mode.BOTH);


        initData();
        adapter = new RoomAdapter(RoomAddressActivity.this, datas);

        layoutRecyclerview.setLayoutManager(new LinearLayoutManager(RoomAddressActivity.this));
        layoutRecyclerview.setAdapter(adapter);

        layoutRecyclerview.addOnItemTouchListener(new RecyclerViewItemTouchListener(layoutRecyclerview,new RecyclerViewItemTouchListener.OnItemClickListener(){

            @Override
            public void onItemClick(View view) {
                int position = layoutRecyclerview.getChildViewHolder(view).getPosition();
                LogUtil.e("position--", position);
                Room roomTemp = datas.get(position);
                if (null != fromPage && fromPage.contentEquals(Const.FROM_WRITE_ORDER)){
                    CoreEventCenter.postObject(EventConst.SELECT_ROOM_OVER, roomTemp);
                } else {
                    setResult(0, getIntent().putExtra("room", roomTemp));
                }
                finish();

            }

            @Override
            public void onItemLongClick(View view) {

            }
        }));

    }

    @Override
    public void alreadyBindBaseViewModel() {
        super.alreadyBindBaseViewModel();

        requestParams = BaseServiceParams.getRoomName();
        doTask(BaseServiceMediator.GET_ROOM_NAME);
    }

    private void initData() {
        datas = new ArrayList<>();
    }


    //    @OnClick({R.id.address_default_tv, R.id.address_delete_tv, R.id.address_edit_tv, R.id.address_add_btn})
    //    public void onClick(View view) {
    //        switch (view.getId()) {
    //            case R.id.address_default_tv:
    //                break;
    //            case R.id.address_delete_tv:
    //                showAlertDialogue("取消", "是否确认删除地址", "删除", new DialogInterface.OnClickListener() {
    //                    @Override
    //                    public void onClick(DialogInterface dialog, int which) {
    //
    //                    }
    //                });
    //                break;
    //            case R.id.address_edit_tv:
    //                break;
    //            case R.id.address_add_btn:
    //                //                requestParams = BaseServiceParams.queryCarAddress("20", "1");
    //                //                doTask(BaseServiceMediator.QUERY_CAR_ADDRESS);
    //                presentController(NewAddressActivity.class);
    //                break;
    //        }
    //    }

    @Override
    public void requestSuccess(CoreServiceResponse response) {
        super.requestSuccess(response);
        if (response.getRequestCode() == BaseServiceMediator.GET_ROOM_NAME_CODE) {
            GetRoomNameResponse queryCarAddressResponse = (GetRoomNameResponse) response.getResponse();
            datas = queryCarAddressResponse.getInfo();
            adapter.setDatas(datas);
            adapter.notifyDataSetChanged();
        }
    }


}
